package com.xbts.system.module.sms;

import com.cloud.apigateway.sdk.utils.Client;
import com.cloud.apigateway.sdk.utils.Request;
import com.xbts.common.constant.Constants;
import com.xbts.common.utils.StringUtils;
import com.xbts.common.utils.http.SSLCipherSuiteUtil;
import com.xkcoding.http.HttpUtil;
import com.xkcoding.http.config.HttpConfig;
import com.xkcoding.http.support.httpclient.HttpClientImpl;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.*;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;

/**
 * 通用http发送方法
 *
 * @author xbts
 */
public class HttpUtils {
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * 向指定 URL 发送POST方法的请求
     * @param url 发送请求的 URL
     * @param method 请求方法
     * @param body 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static HttpEntity sendHuaWeiCloud(String url, String method, String body) {
        Request request = new Request();
        try {
            request.setKey("3G1QX06JA3AKTX9X7D15");
            request.setSecret("R0s6wuhT1vbzrkOfm7Z9AsaVtu9673m4bYWewfEi");
            request.setMethod(method);
            request.setUrl(url);
            request.addHeader("Content-Type", "text/plain");
            if (StringUtils.isNotEmpty(body)) {
                request.setBody(body);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
        CloseableHttpClient client = null;
        HttpEntity resEntity = null;
        try {
            HttpRequestBase signedRequest = Client.sign(request, Constants.SIGNATURE_ALGORITHM_SDK_HMAC_SHA256);
            client = (CloseableHttpClient) SSLCipherSuiteUtil.createHttpClient(Constants.INTERNATIONAL_PROTOCOL);
            HttpResponse response = client.execute(signedRequest);
            resEntity = response.getEntity();
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    log.error(e.getMessage());
                }
            }
        }
        return resEntity;
    }

    public static Response sendHuaWeiCloudByGet(String url, String token){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        okhttp3.Request request = new okhttp3.Request.Builder()
                .url(url)
                .method("GET", null)
                .addHeader("X-Auth-Token", token)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return response;
    }
}